package com.kcyu.sfherp.controller;

import com.kcyu.sfherp.entity.Efficiency;
import com.kcyu.sfherp.entity.SumStatistic;
import com.kcyu.sfherp.mapper.ms.EfficiencyMapper;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.*;

@RestController
@Api(tags="效率分析接口")
@RequestMapping("/efficiency")
public class EfficiencyController {

    @Autowired
    private EfficiencyMapper efficiencyMapper;

    @GetMapping("/query")
    public Map<Object,Object> getZDEfficiency(){
        Date date = new Date();
        String strDateFormat = "yyyy-MM-dd";
        SimpleDateFormat sdf = new SimpleDateFormat(strDateFormat);
        String startTime = sdf.format(date);
        String endTime = sdf.format(date.getTime()+24*60*60*1000);
        Efficiency zd = efficiencyMapper.analysisZD(startTime,endTime);
        Efficiency dd = efficiencyMapper.analysisDD(startTime,endTime);

        HashMap<Object, Object> map1 = new HashMap<>();
        map1.put("zd",zd);
        map1.put("dd",dd);
        HashMap<Object, Object> map = new HashMap<>();
        map.put("code",200);
        map.put("data",map1);
        return map;
    }

    @GetMapping("/analysis")
    public Map<Object,Object> analysisCount(){
        Map<Object, Object> map = new HashMap<>();
        ArrayList<Object> listzd = new ArrayList<>();
        ArrayList<Object> listdd = new ArrayList<>();
        for (int i = 0; i < 7; i++) {
            Date date = new Date();
            String strDateFormat = "yyyy-MM-dd";
            SimpleDateFormat sdf = new SimpleDateFormat(strDateFormat);
            String startTime = sdf.format(date.getTime()-i*24*60*60*1000);
            String endTime = sdf.format(date.getTime()-(i-1)*24*60*60*1000);
            Efficiency zd = efficiencyMapper.analysisZD(startTime,endTime);
            Efficiency dd = efficiencyMapper.analysisDD(startTime,endTime);
            Map<Object, Object> mapzd = new HashMap<>();
            Map<Object, Object> mapdd = new HashMap<>();
            mapzd.put("date",startTime);
            mapzd.put("counter",zd.getCounter());
            mapzd.put("totalje",zd.getTotalje());
            mapdd.put("date",startTime);
            mapdd.put("counter",dd.getCounter());
            mapdd.put("totalje",dd.getTotalje());
            listzd.add(mapzd);
            listdd.add(mapdd);
        }
        HashMap<Object, Object> map1 = new HashMap<>();
        map1.put("zd",listzd);
        map1.put("dd",listdd);
        map.put("code",200);
        map.put("data",map1);
        return map;
    }

    @GetMapping("/getTodayEffi")
    public Map<Object,Object> getTodayOpmanZDEffi(){
        Date date = new Date();
        String strDateFormat = "yyyy-MM-dd";
        SimpleDateFormat sdf = new SimpleDateFormat(strDateFormat);
        String startTime = sdf.format(date);
        String endTime = sdf.format(date.getTime()+24*60*60*1000);
        List<Efficiency> list = efficiencyMapper.getTodayOpmanZDEffi(startTime, endTime);
        List<Efficiency> list2 = efficiencyMapper.getTodayOpmanDDEffi(startTime, endTime);
        HashMap<Object, Object> map = new HashMap<>();
        map.put("zd",list);
        map.put("dd",list2);
        HashMap<Object, Object> resultmap = new HashMap<>();
        resultmap.put("code",200);
        resultmap.put("data",map);
        return resultmap;
    }

    @RequestMapping(value = "/getSumStatistic", method = {RequestMethod.POST})
    public Map<Object,Object> getSumStatistic(@RequestBody Map info){
        String startTime = (String) info.get("startTime");
        String endTime = (String) info.get("endTime");
        List<SumStatistic> list = efficiencyMapper.getSumStatisic(startTime, endTime);
        HashMap<Object, Object> map = new HashMap<>();
        map.put("code", 200);
        map.put("data", list);
        return map;
    }
}
